package com.cwvs.controller.admin;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.cwvs.helper.UrlHelper;
import com.cwvs.services.AppAreaService;
import com.cwvs.services.AppStoreService;
import com.cwvs.services.AppUserService;
import com.cwvs.utils.CommonUtils;
import com.cwvs.utils.JsonUtil;
import com.cwvs.utils.Pages;

/**
 * 店铺医院Controller
 * 
 * @author Administrator
 * 
 */
@Controller
@RequestMapping("/admin")
public class AdminAppStoreController {
	@Resource
	private AppUserService appUserService;
	@Resource
	private AppStoreService appStoreService;
	@Resource
	private AppAreaService appAreaService;
	@Resource
	private UrlHelper urlHelper;
	@RequestMapping("/store_view")
	public ModelAndView storeView(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("app_store/app_store_list");
		return mv;
	}
	@RequestMapping("/store_info")
	public ModelAndView storeInfo(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("app_store/app_store_view");
		String id=request.getParameter("id");
		mv.addObject("model",appStoreService.getStoreInfoById(id));
		return mv;
	}

	@RequestMapping("/store_list")
	public @ResponseBody
	Map<String, Object> storeList(HttpServletRequest request,
			HttpServletResponse response) {
		Pages page = new Pages();
		String limit = request.getParameter("limit");
		String start = request.getParameter("start");
		page.setPageSize(Integer.parseInt(limit));
		page.setCurrentPage(Integer.parseInt(start) / Integer.parseInt(limit)
				+ 1);
		HashMap<String, Object> paramsMap = new HashMap<String, Object>();
		String province_id=request.getParameter("province_id");
		String city_id=request.getParameter("city_id");
		String county_id=request.getParameter("county_id");
		String street_id=request.getParameter("street_id");
		paramsMap.put("PROVINCE_ID", province_id);
		paramsMap.put("CITY_ID", city_id);
		paramsMap.put("COUNTY_ID", county_id);
		paramsMap.put("STREET_ID", street_id);
		String store_type=request.getParameter("store_type");
		if(StringUtils.equalsIgnoreCase("-1", store_type)){
			store_type="";
		}
		paramsMap.put("STORE_TYPE", store_type);
		paramsMap.put("CODE", request.getParameter("code"));
		paramsMap.put("NAME", request.getParameter("name"));
		Pages rePage = appStoreService.storeList(paramsMap, page);
		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("rows", rePage.getPage());
		ret.put("totalCount", rePage.getTotalRecord());
		return ret;
	}
	@RequestMapping("/store_edit")
	public ModelAndView storeEdit(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("app_store/app_store_edit");
		mv.addObject("provinces",JsonUtil.toJson(appAreaService.areaList("0")));
		return mv;
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping("/store_save")
	public @ResponseBody
	Map<String, Object> storeSave(HttpServletRequest request,
			HttpServletResponse response, @RequestParam("photo-path") MultipartFile file) {
		Map<String,Object> map=new HashMap<String,Object>();
		Enumeration paramNames = request.getParameterNames();
		while (paramNames.hasMoreElements()) {
			String paramName = (String) paramNames.nextElement();
			String[] paramValues = request.getParameterValues(paramName);
			if (paramValues.length == 1) {
				String paramValue = paramValues[0];
				if (paramValue.length() != 0) {
					map.put(paramName.toUpperCase(), paramValue);
				}
			}
		}
		List<String> fileTypes=new ArrayList<>();
		fileTypes.add("jpg");
		fileTypes.add("gif");
		fileTypes.add("png");
		fileTypes.add("jpeg");  
        fileTypes.add("bmp");  
        map.put("PIC_URL", CommonUtils.getFile(urlHelper.getUploadFile(),file, "store", fileTypes)); 
		Map<String,Object> res=new HashMap<String,Object>();
		boolean b=appStoreService.saveOrUpdate(map);
		res.put("errormsg", b?"":"保存失败");
		res.put("success", b);
		return res;
	}
}
